# 商品分类文件
from datetime import datetime
from django.core.paginator import Paginator
from django.shortcuts import render
# Create your views here.
from pet_admin.models import GoodType


def find(request, pageIndex=1):
    """
        查询全部商品类型视图
    """
    goodTypeObject = GoodType.objects  # 实例化User对象
    good_type_data = goodTypeObject.filter(type_status__lt=9).order_by('type_id')  # 过滤出来状态小于9的数据
    searchKey = []
    # 获取并判断搜索条件
    key = request.GET.get("searchKey", None)  # 获取表单提交数据
    if key:
        good_type_data = good_type_data.filter(type_name__contains=key)
        searchKey.append("searchKey=" + key)

    # 分页处理
    pageIndex = int(pageIndex)
    page = Paginator(good_type_data, 10)  # 每页多少条
    maxPage = page.num_pages  # 获取最大页数
    # 处理页码越界
    if pageIndex > maxPage:
        pageIndex = maxPage
    if pageIndex < 1:
        pageIndex = 1
    pageTypes = page.page(pageIndex)  # 获取当前页数据
    pages = page.page_range  # 获取页码列表信息
    context = {"types": pageTypes, "pages": pages, "pageIndex": pageIndex, "maxPage": maxPage,
               "searchKey": searchKey}

    return render(request, 'pet_admin/good_manage/good_type/index.html', context)


def add(request):
    """
        添加商品分类视图
    """
    return render(request, "pet_admin/good_manage/good_type/add.html")


def insert(request):
    """
        执行添加程序方法
    """
    try:
        goodType = GoodType()
        goodType.type_name = request.POST["type_name"]
        goodType.type_createtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 类别创建时间
        goodType.type_updatetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 类别修改时间
        goodType.save()
        context = {"info": "添加成功"}

    except Exception as err:
        print(err)
        context = {"info": "添加失败"}

    return render(request, "pet_admin/info.html", context)


def edit(request, type_id):
    """
        修改商品分类信息视图
    """
    try:
        goodType = GoodType.objects.get(type_id=type_id)
        context = {"good_type": goodType}
        return render(request, "pet_admin/good_manage/good_type/edit.html", context)
    except Exception as err:
        print(err)
        context = {"info": "没有找到要修改的信息"}
        return render(request, "pet_admin/info.html", context)


def update(request, type_id):
    """
        执行修改程序的方法
    """
    try:
        print(1)
        goodType = GoodType.objects.get(type_id=type_id)
        goodType.type_name = request.POST["type_name"]
        goodType.type_updatetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 用户修改时间
        goodType.save()
        context = {"info": "修改成功"}

    except Exception as err:
        print(err)
        context = {"info": "修改失败"}

    return render(request, "pet_admin/info.html", context)


def deluser(request, type_id):
    """
       执行删除程序的方法
    """
    try:
        goodType = GoodType.objects.get(type_id=type_id)
        goodType.type_status = 9
        goodType.type_updatetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        goodType.save()
        context = {"info": "删除成功"}

    except Exception as err:
        print(err)
        context = {"info": "删除失败"}

    return render(request, "pet_admin/info.html", context)
